package org.huolala.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.huolala.dao.CarTypeDao;
import org.huolala.dao.OrderDao;
import org.huolala.pojo.Driver;
import org.huolala.pojo.Order;
import org.huolala.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/order")
public class OrderController {
	@Resource
	private OrderDao orderDao;
	@Resource
	private CarTypeDao carTypeDao;

	public void setCarTypeDao(CarTypeDao carTypeDao) {
		this.carTypeDao = carTypeDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add() throws Exception {
		return "addOrder";
	}
	
	@RequestMapping("/view")
	public String view(Integer id,ModelMap modelMap) throws Exception {
		modelMap.put("order",orderDao.getById(id));
		return "driver/view";
	}
	
	@RequestMapping("/jiedan")
	public String jiedan(Order order, HttpSession session) throws Exception {
		Driver driver = (Driver)session.getAttribute("login_driver");
		order.setDriver(driver);
		orderDao.jiedan(order);
		return "redirect:/driver/main";
	}
	
	@RequestMapping("/wancheng")
	public String wancheng(Integer id) throws Exception {
		orderDao.wancheng(id);
		return "redirect:/user/main";
	}
	
	@RequestMapping(value="add",method=RequestMethod.POST)
	public String add(Order order,HttpSession session) throws Exception {
		orderDao.add(order);
		order.setCarType(carTypeDao.getById(order.getCarType().getId()));
		session.setAttribute("newOrder", order);
		return "redirect:/order/confirm";
	}
	
	@RequestMapping("/confirm")
	public String confirm() throws Exception {
		return "order_confirm";
	}
	
	@RequestMapping("/ok")
	public String ok(Order order) throws Exception {
		orderDao.confirmOrder(order);
		return "redirect:/user/main";
	}
	
	/**
	 * 用户登录查看自己的订单列表
	 * @param offset
	 * @param limit
	 * @param condition
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/user/list",produces="application/json")
	@ResponseBody
	public Map<String, Object> list(@RequestParam(defaultValue="0") int offset,@RequestParam(defaultValue="10") int limit, 
			Order condition, HttpSession session) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		Users user = (Users)session.getAttribute("login_user");
		condition.setUsers(user);
		
		List<Order> list = orderDao.getListByCondition(offset, limit, condition);
		int total = orderDao.getCountByCondition(condition);
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 司机登录，查看所有没有被抢的订单
	 * @param offset
	 * @param limit
	 * @param condition
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list",produces="application/json")
	@ResponseBody
	public Map<String, Object> notlist(@RequestParam(defaultValue="0") int offset,@RequestParam(defaultValue="10") int limit, 
			Order condition, HttpSession session) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		condition.setState(0);
		
		List<Order> list = orderDao.getListByCondition(offset, limit, condition);
		int total = orderDao.getCountByCondition(condition);
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 司机登录，查看自己所有抢到的订单
	 * @param offset
	 * @param limit
	 * @param condition
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/driver/list",produces="application/json")
	@ResponseBody
	public Map<String, Object> driverlist(@RequestParam(defaultValue="0") int offset,@RequestParam(defaultValue="10") int limit, 
			Order condition, HttpSession session) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		Driver driver = (Driver)session.getAttribute("login_driver");
		condition.setDriver(driver);
		
		List<Order> list = orderDao.getListByCondition(offset, limit, condition);
		int total = orderDao.getCountByCondition(condition);
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@RequestMapping("/driver/own")
	public String own() throws Exception {
		return "driver/mylist";
	}
}







